var net = require("../../utils/net.js");
var app = getApp();
var utils = require("../../utils/util.js");
const date = new Date()
const years = []
const months = []
const days = []

for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}
var app = getApp()
Page({
  data: {
    achievement: '',
    buy_count: "",
    buy_again_count: "",
    up_count: "",
    new_members: "",
    out_menmber: "",
    subscribe_count: "",
    follow_count: "",
    currentTab: 0,
    years: years,
    year: date.getFullYear(),
    months: months,
    month: 2,
    days: days,
    day: 2,
    array1:[],
    array: [],
    value: [9999, 1, 1],
    index: 0,
    date: '2017-01',
    datee:"2017-01",
    selectPerson: true,
    selectArea: false,
    boss_id: wx.getStorageSync("name")["boss_id"],
    ptindex:0,
    shop_id: wx.getStorageSync("sss"),
    mc_id: wx.getStorageSync("mc_a"),
  },
  bindChange: function (e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],

    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindDateChang: function (e) {
    this.setData({
      datee: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  ptchange: function (e) {
    console.log(e.detail.value)
    this.setData({
      ptindex: e.detail.value
    })
  },
  search: function () {
    this.getmc();
  },
  sear:function(){
    this.pc();
  },
  anniu: function () {
    var that = this;
    that.mc();
    

  },
  mc: function () {
    var that = this;
    net.request({
      url: "/McStatistic",
      data: {
        boss_id: wx.getStorageSync("name")["boss_id"],
        shop_id: wx.getStorageSync("sss"),
        month: that.data.date,
        mc_id:that.data.array[that.data.index].id,

      },
      success: function (res) {
        if (res.data.flag) {
          that.setData({
            achievement: res.data.data.achievement || 0,
            buy_count: res.data.data.buy_count || 0,
            buy_again_count: res.data.data.buy_again_count || 0,
            up_count: res.data.data.up_count || 0,
            new_members: res.data.data.new_members || 0,
            out_menmber: res.data.data.out_menmber || 0,
            subscribe_count: res.data.data.subscribe_count || 0,
            follow_count: res.data.data.follow_count || 0

          })
        }

      },
    })

  },

  onLoad: function (options) {
  
  },
  //  第二的一个页面
  clickTabb: function (e) {
    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
       
      })
    }
  },
  swiperTab: function (e) {

    var that = this;
    that.setData({
      currentTab: e.detail.current
    });
    wx.showLoading({ duration: 2000})
   
    if (e.detail.current == 1) {
      that.pc();
    }
    else{
     that.getmc();
    }
  },
//pc
pc:function(){
  var that = this;
  net.request({
    url: '/GetPtList',
    data: {
      shop_id: wx.getStorageSync("sss"),
      boss_id: wx.getStorageSync("name")["boss_id"],
    },
    success: function (res) {
      if (res.data.flag) {
        that.setData({
          array1: res.data.data,
        })
        wx.showLoading({ duration: 1000 })
        net.request({
          url: "/PtStatistic",
          data: {
            boss_id: wx.getStorageSync("name")["boss_id"],
            shop_id: wx.getStorageSync("sss"),
            month: that.data.datee,
            pt_id: that.data.array1[that.data.ptindex].id

          },
          success: function (res) {
            if (res.data.flag) {
              that.setData({
                achievement: res.data.data.achievement || 0,
                member_count: res.data.data.member_count || 0,
                back_course_count: res.data.data.back_course_count || 0,
                withdraw_count: res.data.data.withdraw_count || 0,
                new_course_count: res.data.data.new_course_count|| 0,
                consumer_course_count: res.data.data.consumer_course_count || 0,
                subscribe_count: res.data.data.subscribe_count || 0,
                follow_count: res.data.data.follow_count || 0
              })
            }
          },
        })
      } else {
        that.setData({
          array1: []

        })
      }

    }
  })
},
  //滑动切换
  //点击切换
  clickTab: function (e) {

    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    if (e.detail.current == 1) {
      that.pc();
    }
    else {
      that.getmc();
    }
  },
  bindPickerChange(e) {
    this.getmc();
    this.setData({
      index: e.detail.value
    })
    wx.setStorageSync("mc_a", this.data.array[e.detail.value].id)
  },
  //pt
  ptchange(e) {
    this.pc();
    this.setData({
      ptindex: e.detail.value
    })
    wx.setStorageSync("mc_a", this.data.array1[e.detail.value].id)
  },
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  }, onShow() {

//修改：
    var that = this;
    var month = new Date().getMonth() + 1;

    if (month < 10) {
      month = "0" + month
    }
    var datee = new Date().getFullYear() + "-" + month;
    var time = utils.formatTime(new Date());
    this.setData({
      datee: datee
    });



    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
    var month = new Date().getMonth() + 1;

    if (month < 10) {
      month = "0" + month
    }
    var date = new Date().getFullYear() + "-" + month;
    var time = utils.formatTime(new Date());
    this.setData({
      date: date
    });

    if (this.data.currentTab== 1) {
      that.pc();
    }
    else {
      that.getmc();
    }

  }
  , getmc() {
    var that = this;
    wx.showLoading({ duration: 1000})
    net.request({
      url: '/GetMcList',
      data: {
        shop_id: wx.getStorageSync("sss"),
        boss_id: wx.getStorageSync("name")["boss_id"],
      },
      success: function (res) {
        if (res.data.flag) {
          that.setData({
            array: res.data.data,

          })

        }that.mc();
      }
      
    })
  }
 
     
})